package com.example.denglu.config;



import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ValidError handleMethodArgumentNotValidException(MethodArgumentNotValidException e , HttpServletResponse resp){
        resp.setStatus(412);
        FieldError fe = e.getBindingResult().getFieldError();
        ValidError validError = new ValidError(fe.getField(), fe.getDefaultMessage());
        log.error(validError.toString());


        return validError;
    }
}
